<template>
  <div>
    <tree-transfer
      ref="treeTransfer"
      :title="['未选项','已选项']"
      :arrayToTree="false"
      :filter="true"
      :watchRight='watchRight'
      @getRight='getRight'
      node_key="_id"
      :default-props="{label: 'label', children: 'children'}"
    ></tree-transfer>
  </div>
</template>   
<script>
import treeTransfer from '@/components/ui-modules/treeTransfer/treeTransfer.vue'
import { filterAllCategory } from '@/utils/customerUtils.js'
import { deepClone } from '@/utils/m7Utils'
export default {
  name: 'ownertree',
  props: {
    _scopeArray: Array,
    watchRight: Boolean,
  },
  data() {
    return {
      scopeNoticeArray: [],
      inputValue: '',
      defaultExpandedKeys: ['common'],
      optionData: [],
      popoverVisable: false,
      isNotice: true,
    }
  },
  methods: {
    setdata(data, todata) {
      let generateFromDataAndToData = function (options, pId, toData) {
        for (let i = 0; i < options.length; i++) {
          let optionItem = options[i]
          let children = optionItem.children || []
          if (children.length !== 0) {
            generateFromDataAndToData(children, optionItem._id, toData)
            let position = toData.indexOf(optionItem._id)
            if (position !== -1) {
              toData.splice(position, 1)
            }
          }
          optionItem.pid = pId
        }
      }
      generateFromDataAndToData(data, 0, todata)
      console.error(data)
      this.$refs.treeTransfer.setCheckdkeys(data, todata)
    },
    getRight(val){
      this.$emit('getRight',val)
    }
  },
  beforeMount() {
    Promise.all([
      this.$store.dispatch('getCache', { type: 'depts' }),
      this.$store.dispatch('getCache', { type: 'agents' }),
      this.$store.dispatch('getCache', { type: 'custCategorys' }),
    ]).then(([depts, agents, custCategorysCache]) => {
      let optionData = []
      let custCategorys = filterAllCategory()
      let emptyOption = {
        _id: 'all',
        label: this.$t('public.all'),
        type: 'ownercom',
      }
      let commonOption = {
        _id: 'common',
        label: this.$t('public.common'),
        children: [
          { _id: 'myself', type: 'ownercom', label: this.$t('public.myself') },
          {
            _id: 'my-subordinates',
            type: 'ownercom',
            label: this.$t('public.myAndMylower'),
          },
          {
            _id: 'my-department',
            type: 'ownercom',
            label: this.$t('public.ownerDept'),
          },
          {
            _id: 'my-subordinates-dept',
            type: 'ownercom',
            label: this.$t('public.ownerAndMyLowerDept'),
          },
        ],
      }
      let noOwner = {
        _id: 'NA',
        type: 'owner',
        label: this.$t('customer.noOwner'),
      }
      let _custCategorys = custCategorys.map((category) => ({
        _id: category._id,
        type: 'owner',
        label: category.displayName,
        displayName: category.cName,
      }))
      let _agents = agents.map((agent) => ({
        _id: agent._id,
        type: 'owner',
        label: `${agent.displayName}[${agent.exten}]`,
        displayName: agent.displayName,
        pinyin: agent.pinyin,
      }))
      let customerOwners = []
      let otherOwners
      if (this.isNotice) {
        customerOwners = [..._custCategorys, ..._agents]
        otherOwners = [..._agents]
      } else {
        customerOwners = [noOwner, ..._custCategorys, ..._agents]
        otherOwners = [noOwner, ..._agents]
      }
      let ownerOption = {
        _id: 'owner',
        label:
          this.businessType === 'customer'
            ? this.$t('customer.customerOwner')
            : this.$t('customer.customerWorker'),
        children:
          this.businessType === 'customer' ? customerOwners : otherOwners,
      }
      let _depts = depts.filter((dept) => dept.pId !== '')
      _depts = _depts.map((dept) => ({
        _id: dept._id,
        type: 'ownerdep',
        label: dept.name,
      }))
      let deptOption = {
        _id: 'dept',
        label: this.$t('customer.customerDept'),
        children: _depts,
      }
      if (this.isNotice) {
        optionData.push(...[emptyOption, ownerOption, deptOption])
      } else {
        optionData.push(...[emptyOption, commonOption, ownerOption, deptOption])
      }
      this.optionData = optionData
      let array= [...this._scopeArray]
      this.setdata(this.optionData, array)
      console.error(array)
      // this.$emit('tree-data', this.optionData, this.scopeNoticeArray)
      // this.$on('resetValue', this.resetValue)
    })
  },
  components: {
    treeTransfer,
  },
  watch:{
    _scopeArray(val){
      console.log(this.optionData)
      let array= [...val]
      this.setdata(this.optionData, array)
    }
  }
}
</script>